/* Copyright 2007 Gearbox Software - http://www.gearboxsoft.com
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
     http:www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.*/
namespace GearBlog.Web.Models
{
	using System;
	using System.Text;
	using Castle.ActiveRecord;
	using Castle.Components.Validator;
	using GearBlog.Web.Validators;
	using Iesi.Collections.Generic;
	using ValidateEmailAttribute=Castle.Components.Validator.ValidateEmailAttribute;

	/// <summary>
    /// Summary description for Setting
    /// </summary>
    [ActiveRecord]
    public class Setting:ExtendedActiveRecordBase<Setting>
    {
		private static Setting _setting;

        private int _numOfDaysBeforeArchive;
    	private bool _registeredUserCommentOnly;
    	private bool _registeredUserOnly;
    	private string _title;
    	private string _description;
    	private string _ownerEmail;
		private bool _allowPublicRegistration;
		private ISet<Role> _defaultRoles=new HashedSet<Role>();
		private bool _enableNotifier=false;
		private bool _pingWebLogs=false;
		private string _proxyHost;
		private int _proxyPort;
		private int _trackBackTimeOut;
		private bool _moderateComments;
		private bool _submitTrackBack;
		private bool _receiveTrackBack;

		public static Setting Get(bool forceRefresh)
		{
			if (_setting == null || forceRefresh)
				_setting = FindFirst();
			return _setting;
		}

		public static Setting Get()
		{
			return Get(false);
		}
		
		[Property(NotNull = true),ValidateEmailAttribute, ValidateNonEmpty]
    	public string OwnerEmail
    	{
    		get { return _ownerEmail; }
    		set { _ownerEmail = value; }
    	}

    	[Property]
    	public string Title
    	{
    		get { return _title; }
    		set { _title = value; }
    	}

    	[Property]
        public int NumOfDaysBeforeArchive
        {
            get { return _numOfDaysBeforeArchive; }
            set { _numOfDaysBeforeArchive = value; }
        }
		[Property]
    	public bool RegisteredUserCommentOnly
    	{
    		get { return _registeredUserCommentOnly; }
    		set { _registeredUserCommentOnly = value; }
    	}

		[Property]
    	public bool RegisteredUserOnly
    	{
    		get { return _registeredUserOnly; }
    		set { _registeredUserOnly = value; }
    	}
		
		[Property]
    	public string Description
    	{
    		get { return _description; }
			set { _description = value; }
    	}

		[Property]
		public bool AllowPublicRegistration
		{
			get { return _allowPublicRegistration; }
			set { _allowPublicRegistration = value; }
		}

		[ValidateRoleList]
		public string DefaultRoleNames
		{
			get
			{
				StringBuilder sb=new StringBuilder();
				foreach (Role role in _defaultRoles)
				{
					sb.AppendFormat("{0},", role.Name);
				}
				return sb.ToString().TrimEnd(',');
			}
			set
			{
				string[] roleNames = value.Split(',');
				
				HashedSet<Role> newRoles = new HashedSet<Role>();

				foreach (string roleName in roleNames)
				{
					Role role = Role.FindByName(roleName.Trim());
					if (role == null)
						throw new ArgumentException(string.Format("Role name not found: {0}", roleName));
					else
						newRoles.Add(role);

				}

				_defaultRoles.AddAll(newRoles);
				_defaultRoles = _defaultRoles.Intersect(newRoles);

			}
		}

		[HasAndBelongsToMany(typeof(Role),Table="SettingRole", ColumnKey = "SettingId", ColumnRef = "RoleId")]
		public ISet<Role> DefaultRoles
		{
			get { return _defaultRoles; }
			set { _defaultRoles = value; }
		}
		[Property(NotNull=true)]
		public bool EnableNotifier
		{
			get { return _enableNotifier; }
			set { _enableNotifier = value;}
		}
		[Property(NotNull=true)]
		public bool PingWebLogs
		{
			get { return _pingWebLogs; }
			set { _pingWebLogs = value; }
		}

		//Proxy settings for XML-RPC

		[Property]
		public string ProxyHost
		{
			get { return _proxyHost; }
			set { _proxyHost = value; }
		}
		[Property]
		public int ProxyPort
		{
			get { return _proxyPort; }
			set { _proxyPort = value; }
		}

		[Property]
		public int TrackBackTimeOut
		{
			get { return _trackBackTimeOut; }
			set { _trackBackTimeOut = value; }
		}

		[Property]
		public bool ModerateComments
		{
			get { return _moderateComments; }
			set { _moderateComments = value; }
		}

		[Property]
		public bool SubmitTrackBack
		{
			get { return _submitTrackBack; }
			set { _submitTrackBack = value; }
		}

		[Property]
		public bool ReceiveTrackBack
		{
			get { return _receiveTrackBack; }
			set { _receiveTrackBack = value; }
		}
    }
}
